import json
import requests
from bs4 import BeautifulSoup
def save_file(filename,content):
    with open("论坛/"+filename+".json","w+",encoding="utf-8") as f:
        f.write(json.dumps(content,ensure_ascii=False,indent=2))
def do_requset(url):
    response = requests.get(url)
    response.encoding = "utf-8"
    return response.text
def parse_page(html):
    # 解析网页
    soup = BeautifulSoup(html, features="html.parser")
    # 找到所有的item
    items = soup.select(".new.forumtit")
    data_t = []
    # 循环
    for item in items:
        # 标题
        title = item.select(".s.xst")[0].text
        print(title)
        # 作者
        author = item.select(".foruminfo>.z>a>span")[0].text
        print(author)
        date = item.select(".foruminfo > i.z > span:nth-child(2)")[0].text
        print(date)
        d = {"标题":title,"作者":author,"日期":date}
        data_t.append(d)
    return data_t
def create_url(start_page,end_page):
    base_url = "https://bbs.itheima.com/forum-425-"
    for i in range(start_page,end_page+1):
        url = base_url+str(i)+".html"
        html = do_requset(url)
        data = parse_page(html)
        save_file(str(i),data)
if __name__ == '__main__':
    create_url(1,5)